#include "clTestBase.h"


int clTestBase::run(std::string _kernel,cl::Buffer& buffer){
	
	try
	{
		*devices= context->getInfo<CL_CONTEXT_DEVICES>();

		program->build(*devices);
		
		cl::Kernel kernel(*program, _kernel.c_str(), NULL);
		
		cl::CommandQueue queue(*context, *devices[0], CL_QUEUE_PROFILING_ENABLE, NULL);
		cl::KernelFunctor func = kernel.bind(
										queue,
										*global_range,
										*local_range
									);
		
		cl::Event event=func(buffer);
		event.wait();

		{
			cl_ulong _start,_end;
			event.getProfilingInfo(CL_PROFILING_COMMAND_START,&_start);
			event.getProfilingInfo(CL_PROFILING_COMMAND_END,&_end);
			std::cout<<_start<<"->"<<_end<<"="<<(_end-_start)
				<<" ("<<(float)(_end-_start)/(10e+6)<<" msec)"<<std::endl;
		}
		
	}catch (cl::Error err) {
		std::cerr
		<< "ERROR: "
		<< err.what()
		<< "("
		<< err.err()
		<< ")"
		<< std::endl;
	}
	
	return 0;
}

std::string clTestBase::loadProgram(){
	
	std::ifstream ifs(filename.c_str());
	
	if(!ifs)
	{
		std::cerr << "Cannot open file '" << filename << "'\n";
		return std::string("");;
	}

	std::ostringstream oss;
	oss << ifs.rdbuf();

	if(!ifs && !ifs.eof())
		std::cerr << "Error reading file '" << filename << "'\n";

	std::string contents(oss.str());
	
	return contents;
}
